#include "rtc.h"
/*****************************************/
/*****************************************/
/*****************************************/
/*****************************************/
/*****************************************/
extern str_Time_t AlarmTime_A;
extern str_Time_t AlarmTime_B;
/*****************************************/
void RTC_Init(void)
{
		rtc_init();  										 //初始化rtc
		rtc_CountUpdate(0);
		rtc_AlarmConfig(AlARM_A,0,0,1);  //配置时间1s进入一次中断 
		NVIC_EnableIRQ(PMU_IRQn);				 //使能PMU中断 
}
/*****************************************/

__attribute__((section("ram_code"))) void pmu_isr(void)  //PMU中断处理函数
{
    uint16_t state = pmu_get_isr_state();//获取pmu中断标志

		if(state & PMU_RTC_ALMA_INT_STATUS)
		{
			rtc_AlarmHandler(state);
			printf("A   rtc_AlarmHandler\r\n");
		}
			if(state & PMU_RTC_ALMB_INT_STATUS)
		{
			rtc_AlarmHandler(state);
			printf("B   rtc_AlarmHandler\r\n");
		}
		
		pmu_clear_isr_state(state);  				//清除pmu中断标志
}
/*****************************************/



/*********************************************************************
 * @fn      rtc_AlarmHandler
 *
 * @brief   Alarm interrupt handler 
 */
void rtc_AlarmHandler(uint16_t state)
{
    if ( state & PMU_RTC_ALMA_INT_STATUS) 
    {
        pmu_clear_isr_state(PMU_RTC_ALMA_INT_CLR);

        /* Alarm cycle */
        AlarmTime_A.CycleBackup += AlarmTime_A.FirstBackup;

        rtc_AlarmConfig(AlARM_A, 0, 0, AlarmTime_A.CycleBackup);
    }
    else if ( state & PMU_RTC_ALMB_INT_STATUS) 
    {
        pmu_clear_isr_state(PMU_RTC_ALMB_INT_CLR);

        /* Alarm cycle */
        AlarmTime_B.CycleBackup += AlarmTime_B.FirstBackup;

        rtc_AlarmConfig(AlARM_B, 0, 0, AlarmTime_B.CycleBackup);
    }
}
